package com.VolunteerPlatform.Service.VolunteerService.Impl;

import com.VolunteerPlatform.Dao.VolunteerSignDao;
import com.VolunteerPlatform.Entity.VolunteerSign;
import com.VolunteerPlatform.Service.VolunteerService.VolunteerSignService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by DoveXL on 2018/12/15.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class VolunteerSignServiceImpl implements VolunteerSignService{
    @Autowired
    private VolunteerSignDao volunteerSignDao;

    @Override
    public int insertVolunteerSign(String IDCardNumber, String Name, String StartTime, String EndTime, String DateDay, int isFullDay, String AdminID) {
        return volunteerSignDao.insertVolunteerSign(IDCardNumber,Name,StartTime,EndTime,DateDay,isFullDay,AdminID);
    }

    public List<VolunteerSign> getAllVolunteerSign(String IDCardNumber, String Name, String StartTime, String EndTime, String DateDay, int isFullDay, String AdminID)
    {
        String IDCard = "";
        String NameLike="";
        String StartTimeLike="";
        String EndTimeLike = "";
        String DateDayLike = "";
        if(IDCardNumber==null)
        {
            IDCard=null;
        }else
        {
            IDCard = "%"+IDCardNumber+"%";
        }
        if(Name==null)
        {
            NameLike =null;
        }else
        {
            NameLike = "%"+Name+"%";
        }

        if(StartTime==null)
        {
            StartTimeLike =null;
        }else
        {
            StartTimeLike = "%"+StartTime+"%";
        }


        if(EndTime==null)
        {
            EndTimeLike =null;
        }else
        {
            EndTimeLike = "%"+EndTime+"%";
        }

        if(DateDay==null)
        {
            DateDayLike =null;
        }else
        {
            DateDayLike = "%"+DateDay+"%";
        }
        System.out.println(IDCard+" "+NameLike+" "+DateDayLike+" "+" "+StartTimeLike+" "+EndTimeLike+" "+isFullDay);
        return volunteerSignDao.getAllVolunteerSign(IDCard,NameLike,StartTimeLike,EndTimeLike,DateDayLike,isFullDay,AdminID, -1,-1);



    }

    public List<VolunteerSign> getCertainVolunteerSign(String IDCardNumber, String Name, String StartTime, String EndTime,  String StartDay,  String EndDay,  String Unit,  int Page,  int PageList)
    {
        String IDCard= "";
        String NameLike="";
        String UnitLike= "";
        if(IDCardNumber==null)
        {
            IDCard=null;
        }else
        {
            IDCard = "%"+IDCardNumber+"%";
        }
        if(Name==null)
        {
            NameLike =null;
        }else
        {
            NameLike = "%"+Name+"%";
        }

        if(Unit==null)
        {
            UnitLike =null;
        }else
        {
            UnitLike = "%"+Unit+"%";
        }
        return volunteerSignDao.getCertainVolunteerSign(IDCard,NameLike,StartTime,EndTime,StartDay,EndDay,UnitLike,Page,PageList);
    }




    public int updateVolunteerSign(String IDCardNumber,String Date,String Time,String AdminID,String OriginDate)
    {
        int isFullDay=0;
        if(Time.equals("9:00-11:30") || Time.equals("13:30-16:00"))
        {
            isFullDay=0;
        }else if (Time.equals("9:00-16:00"))
        {
            isFullDay=1;
        }
        int index = Time.lastIndexOf("-");
        String StartTime = Time.substring(0,index);
        String EndTime = Time.substring(index+1);
        return volunteerSignDao.updateVolunteerSign(IDCardNumber,Date,StartTime,EndTime,isFullDay,AdminID,OriginDate);
    }


    public int deleteVolunteerSign(String IDCardNumber,String Date,String Time)
    {
        int index = Time.lastIndexOf("-");
        String StartTime = Time.substring(0,index);
        String EndTime = Time.substring(index+1);
        return volunteerSignDao.deleteVolunteerSign(IDCardNumber,Date,StartTime,EndTime);
    }
}
